﻿Imports System.Collections.Specialized

Public Class frmTabSpatialDomain
    Public Shared sDataSetGPoly As StringCollection = New StringCollection()
    Public Shared sCountGPoly As StringCollection = New StringCollection()
    Public Shared sPdelim As String = "£"
    Dim editNode As EditXML = New EditXML()

    Private Sub btnAddPoly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPoly.Click
        Dim iCount As Integer = 0
        Dim iLoop As Integer = 0
        Dim iPolyCount As Integer = 0
        Dim iExclCount As Integer = 0
        Dim s1 As String = ""
        Dim s2 As String = ""
        Dim sList As String = ""
        Dim frmNewGPoly As New frmGPoly

        frmNewGPoly.ShowDialog()
        If (frmNewGPoly.getChanged() = True) Then
            iCount = frmNewGPoly.getPolyCol().Count 'number of strings in the collection
            iExclCount = CInt((iCount - 2) / 2) 'number of exclusion polygons for this data set G-polygon
            For iLoop = 0 To iCount - 1
                sDataSetGPoly.Add(frmNewGPoly.getPolyCol()(iLoop)) 'transfer each string to the local collection
            Next
            sCountGPoly.Add(iExclCount.ToString)
            s1 = frmNewGPoly.getPolyCol()(0)
            s2 = frmNewGPoly.getPolyCol()(1)
            sList = s1 & ": " & Microsoft.VisualBasic.Left(s2, 32)
            sList = sList.Replace(sPdelim, "  ")
            lboxGPoly.Items.Add(sList) 'add data set polygon to list box (outer g-ring)
        End If
        editXMLFile()
    End Sub

    Private Sub btnEditPoly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditPoly.Click
        Dim iSelInd As Integer = 0 'selection index for polygon list box
        Dim iToStart As Integer = 0
        Dim iToDelete As Integer = 0
        Dim iLoop As Integer = 0

        Dim iCount As Integer = 0
        Dim iPolyCount As Integer = 0
        Dim iExclCount As Integer = 0
        Dim s1 As String = ""
        Dim s2 As String = ""
        Dim sList As String = ""

        iCount = lboxGPoly.Items.Count
        iSelInd = lboxGPoly.SelectedIndex
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then
            MessageBox.Show("No polygon selected for editing", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim sToEdit As New StringCollection
        If (iSelInd > 0) Then
            For iLoop = 0 To iSelInd - 1 'count number of collection elements preceding start of polygon to edit
                iToStart += CInt(sCountGPoly(iLoop)) * 2 + 2
            Next
        End If
        iToDelete = 2 + 2 * CInt(sCountGPoly(iSelInd)) 'number of strings in the collection for the polygon to edit
        iPolyCount = iToStart + iToDelete - 1
        For iLoop = iToStart To iPolyCount
            sToEdit.Add(sDataSetGPoly(iLoop)) 'create string collection for polygon to edit
        Next

        Dim frmNewGPoly As New frmGPoly(sToEdit)
        frmNewGPoly.ShowDialog()
        If (frmNewGPoly.getChanged() = True) Then
            'delete the old version
            lboxGPoly.Items.RemoveAt(iSelInd) 'delete polygon from list box
            For iLoop = 1 To iToDelete
                sDataSetGPoly.RemoveAt(iToStart) 'delete type descriptors and descriptions of polygon & exclusions
            Next
            sCountGPoly.RemoveAt(iSelInd) 'delete count of exclusion polygons from its collection

            'append the new version
            iCount = frmNewGPoly.getPolyCol().Count 'number of strings in the collection
            iExclCount = CInt((iCount - 2) / 2) 'number of exclusion polygons for this data set G-polygon
            For iLoop = 0 To iCount - 1
                sDataSetGPoly.Add(frmNewGPoly.getPolyCol()(iLoop)) 'transfer each string to the local collection
            Next
            sCountGPoly.Add(iExclCount.ToString)
            s1 = frmNewGPoly.getPolyCol()(0)
            s2 = frmNewGPoly.getPolyCol()(1)
            sList = s1 & ": " & Microsoft.VisualBasic.Left(s2, 32)
            sList = sList.Replace(sPdelim, "  ")
            lboxGPoly.Items.Add(sList) 'add data set polygon to list box (outer g-ring)
        End If
        btnEditPoly.Enabled = False : btnDelPoly.Enabled = False
        editXMLFile()
    End Sub

    Private Sub btnDelPoly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelPoly.Click
        Dim iSelInd As Integer = 0 'selection index for polygon list box
        Dim iToStart As Integer = 0
        Dim iToDelete As Integer = 0
        Dim iLoop As Integer = 0

        iSelInd = lboxGPoly.SelectedIndex
        If (iSelInd = -1) Then
            MessageBox.Show("No polygon selected for deletion", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            lboxGPoly.Items.RemoveAt(iSelInd) 'delete polygon from list box
            If (iSelInd > 0) Then
                For iLoop = 0 To iSelInd - 1 'count number of collection elements preceding start of polygon to delete
                    iToStart += CInt(sCountGPoly(iLoop)) * 2 + 2
                Next
            End If
            iToDelete = 2 + 2 * CInt(sCountGPoly(iSelInd)) 'number of strings in the collection for the polygon
            For iLoop = 1 To iToDelete
                sDataSetGPoly.RemoveAt(iToStart) 'delete type descriptors and descriptions of polygon & exclusions
            Next
            sCountGPoly.RemoveAt(iSelInd) 'delete count of exclusion polygons from its collection
        End If
        btnEditPoly.Enabled = False : btnDelPoly.Enabled = False
        editXMLFile()
    End Sub

    Private Sub frmTabSpatialDomain_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        editXMLFile()
    End Sub

    Public Sub editXMLFile()
        editNode.Edit("/metadata/idinfo/spdom/descgeog", txtGeoExt.Text.Trim())
        editNode.Edit("/metadata/idinfo/spdom/bounding/westbc", numWestBC.Value.ToString())
        editNode.Edit("/metadata/idinfo/spdom/bounding/eastbc", numEastBC.Value.ToString())
        editNode.Edit("/metadata/idinfo/spdom/bounding/northbc", numNorthBC.Value.ToString())
        editNode.Edit("/metadata/idinfo/spdom/bounding/southbc", numSouthBC.Value.ToString())
        editNode.Remove("/metadata/idinfo/spdom/bounding", "boundalt")
        editNode.Remove("/metadata/idinfo/spdom", "dsgpoly")
        If numAltMax.Value <> 0 Or numAltMin.Value <> 0 Or cboxIDaltDistU.SelectedIndex <> -1 Then
            Dim newNode2 As String = "<altmin>" & numAltMin.Value.ToString() & "</altmin><altmax>" & numAltMax.Value.ToString() & "</altmax><altunits>" & cboxIDaltDistU.SelectedItem & "</altunits>"
            editNode.Add("/metadata/idinfo/spdom/bounding", "/southbc", "boundalt", newNode2)
        End If

        Dim iToStart As Integer = 0
        Dim iToEnd As Integer = 0
        Dim iExPolyCount As Integer = 0
        Dim newNode As String = ""
        For i As Integer = 0 To sCountGPoly.Count() - 1
            newNode = newNode & "<dsgpolyo>"
            If sDataSetGPoly(iToStart) = "string" Then
                newNode = newNode & "<gring>" & sDataSetGPoly(iToStart + 1) & "</gring>"
            Else
                Dim items As String() = sDataSetGPoly(iToStart + 1).Split(sPdelim)
                For j As Integer = 0 To items.GetUpperBound(0)
                    Dim sPoints As String() = Split(items(j))
                    newNode = newNode & "<grngpoin>"
                    newNode = newNode & "<gringlat>" & sPoints(0) & "</gringlat><gringlon>" & sPoints(1) & "</gringlon>"
                    newNode = newNode & "</grngpoin>"
                Next
            End If
            iToStart += 2
            iExPolyCount = CInt(sCountGPoly(i)) 'number of exclusion polygons for particular G-polygon
            iToEnd = iToStart + iExPolyCount * 2 - 1
            For j As Integer = iToStart To iToEnd Step 2
                newNode = newNode & "<dsgpolyx>"
                If (sDataSetGPoly(j) = "string") Then
                    newNode = newNode & "<gring>" & sDataSetGPoly(j + 1) & "</gring>"
                Else
                    Dim items As String() = sDataSetGPoly(j + 1).Split(sPdelim)
                    For k As Integer = 0 To items.GetUpperBound(0)
                        Dim sPoints As String() = Split(items(k))
                        newNode = newNode & "<grngpoin>"
                        newNode = newNode & "<gringlat>" & sPoints(0) & "</gringlat><gringlon>" & sPoints(1) & "</gringlon>"
                        newNode = newNode & "</grngpoin>"
                    Next
                End If
                newNode = newNode & "</dsgpolyx>"
            Next
            If (iToStart < iToEnd) Then iToStart = iToEnd + 1
            newNode = newNode & "</dsgpolyo>"
        Next
        editNode.Add("/metadata/idinfo/spdom", "/bounding", "dsgpoly", newNode)
    End Sub

    Private Sub txtGeoExt_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGeoExt.Leave
        editXMLFile()
    End Sub

    Private Sub lboxGPoly_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxGPoly.SelectedIndexChanged
        If lboxGPoly.SelectedItems.Count() > 0 Then btnEditPoly.Enabled = True : btnDelPoly.Enabled = True Else btnEditPoly.Enabled = False : btnDelPoly.Enabled = False
    End Sub

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("SpatialDomain")
        helpForm.ShowDialog()
    End Sub
End Class